package com.nativetalk.common;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;

public class ValidationMessages {
	private static Logger logger = Logger.getLogger(ValidationMessages.class);
	private static PropertiesConfiguration config = null;

	static {
		try {
			init();
		} catch (ConfigurationException e) {
			throw new RuntimeException("ValidationMessages.properties加载失败");
		}
	}
	public ValidationMessages() {
		/**
		try {
			init();
		} catch (ConfigurationException e) {
			throw new RuntimeException("config.properties加载失败");
		}
		*/
	}

	
	/**
	 * 初始化配置文件并加载配置
	 */
	public static void init() throws ConfigurationException {
		config = new PropertiesConfiguration("ValidationMessages.properties");
		FileChangedReloadingStrategy change = new FileChangedReloadingStrategy();
		// 一分钟刷新一次
		change.setRefreshDelay(60000);
		config.setReloadingStrategy(new FileChangedReloadingStrategy());
	}
	/**
	 *
	 * 读取配置文件中的字符串
	 * @param key 键值
	 * @return 对应的字符串值
	 */
	public static String getValue(String key) {
		// String value = (String)map.get(key);
		if (config == null)
			logger.error("config is null*****************");
		String value = config.getString(key);
		return value;
	}

	/**
	 * 读取配置文件中的字符串(键值为'prefix.key'的形式)
	 * @param prefix 键值前缀
	 * @param key 键值
	 * @return 对应的字符串值
	 */
	public static String getValue(String prefix, String key) {
		// String value = (String)map.get(key);
		String value = config.getString(prefix + "." + key);
		return value;
	}
}
